#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include <process.h>
#include <vector>
#include <time.h>
using namespace std;
vector<int> g_vec;
DWORD WINAPI thread1_function(LPVOID arg);
HANDLE hMutex;

int main()
{
	hMutex = CreateMutex(NULL, false, NULL );
	CreateThread( NULL, 0 , thread1_function
		, 0, 0, NULL );
	while ( 1 )
	{
		time_t nowtime = time(NULL);
		WaitForSingleObject( hMutex, INFINITE);
		g_vec.push_back(nowtime);
		g_vec.push_back(nowtime + 1);
		
		for ( vector<int>::iterator iter = g_vec.begin(); iter != g_vec.end(); iter++ )
		{
			printf("%d ", *iter );
		}
		ReleaseMutex( hMutex );
		printf("\n");
		
		printf("Thread main\n");
		Sleep(1000);
	}
	return 0;
}

DWORD WINAPI thread1_function(LPVOID arg)
{
	while (1)
	{
		
		if ( !g_vec.empty() )
		{
			WaitForSingleObject( hMutex, INFINITE );
			vector<int> temp ;
			temp.assign( g_vec.begin(), g_vec.end() );
			g_vec.clear();
			ReleaseMutex( hMutex );
			for ( vector<int>::iterator iter = temp.begin() ; iter != temp.end(); iter++ )
			{
				printf("thread1_function %d \n", *iter );
				Sleep(200);
			}
			temp.clear();
			
		}
		else
		{
			printf("thread1_function wait 200s \n" );
			Sleep(100);
		}
		
	}
}